/*
    Copyright (c) 2006-2007 Ivaylo Ivanov

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
*/
package com.avalonsql.core.configs;

import javax.naming.OperationNotSupportedException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.InvalidPropertiesFormatException;


public abstract class Config {

    protected String propertiesFile = null;
    // todo make it private!!
    protected Properties properties = new Properties();
    protected boolean changed;

    protected Config() {
    }

    protected Config(String propertiesFile) {
        this.propertiesFile = propertiesFile;
    }

    public boolean isChanged() {
        return changed;
    }

    public Config reload() throws ConfigIOException {
        FileInputStream fileInputStream = null;
        String propertiesFile = getPropertiesFile();
        try {
            fileInputStream = new FileInputStream(propertiesFile);
            properties.loadFromXML(fileInputStream);
            changed = false;
        } catch (FileNotFoundException e) {
            throw new ConfigIOException(propertiesFile + " reloading error",  e);
        } catch (InvalidPropertiesFormatException e) {
            throw new ConfigIOException(propertiesFile + " reloading error",  e);
        } catch (IOException e) {
            throw new ConfigIOException(propertiesFile + " reloading error",  e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                }
            }
        }

        return this;
    }

    public void save() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(getPropertiesFile());
        try {
            properties.storeToXML(fileOutputStream, null);
            fileOutputStream.flush();
            changed = false;
        } finally {
            fileOutputStream.close();
        }
    }

    public void delete() throws OperationNotSupportedException {
        throw new OperationNotSupportedException();
    }

    public Properties getProperties() {
        return properties;
    }

    public String getProperty(String name) {
        if (name == null) {
            throw new IllegalArgumentException("name = null");
        }
        return properties.getProperty(name);
    }

    public void setProperty(String name, String value) {
        if (name == null) {
            throw new IllegalArgumentException("property name = null");
        }
        properties.setProperty(name, value != null ? value : "");
        changed = true;
    }

    public String getPropertiesFile() {
        assert propertiesFile != null : "Not specified properties file!";
        return propertiesFile;
    }

}
